The KeyTech Windows Registry ActiveX control 2.0 simplifies access to the Windows Registry for the Visual Basic programmer. It models the registry using three object classes: RegistryDatabase; RegistryKey; and RegistryValue.
RegistryDatabase represents either the local registry database or the database on another computer. RegistryKey and RegistryValue represent the two types of objects stored in a registry database, namely keys and values.
It also includes a RegistryAccessor object class to simplify the more common operations.
So, for example, the following code sets number, string, multi-string and binary values:
Dim RegDB As New RegistryDatabase
Dim RegKey As RegistryKey
Dim RegValue As RegistryValue
' Create or open the key
Set RegKey = RegDB.CreateKey(REG_HKEY_LOCAL_MACHINE, "\Software\MyCompany\MyProduct")
' Set the values
Set RegValue = RegKey.OpenValue("MyNumber")
RegValue = 1234
Set RegValue = RegKey.OpenValue("MyString")
RegValue = "This is a string"
Set RegValue = RegKey.OpenValue("MyMultiString")
RegValue = Array("Red", "Green", "Blue")
Set RegValue = RegKey.OpenValue("MyBinaryData")
RegValue = Array(1, 2, 3, 4, 5, 6, 7, 8)
Or alternatively, the same operations may be performed using the RegistryAccessor:
Dim Reg As New RegistryAccessor
' Create or open the key and set the values
With Reg
.SubKey = "\Software\MyCompany\MyProduct
.ValueName = "MyNumber"
.Value = 1234
.ValueName = "MyString"
.Value = "This is a string"
.ValueName = "MyMultiString"
.Value = Array("Red", "Green", "Blue")
.ValueName = "MyBinaryData"
.Value = Array(1, 2, 3, 4, 5, 6, 7, 8)
End With
The control encapsulates the most commonly used features of the native Windows registry API including:
The control is intended for use with Visual Basic 5.0 or later releases running under Windows 95/98 or Windows NT 4.0. It may also be used from within VBScript or JScript running under Internet Explorer 4.0 or later releases.
For further information, visit us at http://www.keytech.com.au or send email to info@keytech.com.au.
To report bugs or suggest enhancements for future releases send email to support@keytech.com.au.
This document assumes the reader has an understanding of the Windows registry and the associated Win32 API. For further details refer to the relevant Microsoft documentation.
For users of previous versions of the Windows Registry ActiveX control ensure that it is uninstalled before installing the new version.
Copy the registry DLL to the system directory and register it by running regsvr32 registry.dll. Compile and run the sample Visual Basic programs making sure to set the project references to include the registry control.
To uninstall, run regsvr32 /u registry.dll and remove the DLL.
The RegistryAccessor class may be used for simple accesses to the registry involving setting and querying values. For more complex access use the RegistryDatabase and associated classes.
To use the RegistryAccessor create an object using the new operator or the CreateObject function. Set the ComputerName if a remote computer is being accessed. Set the HKEY if REG_HKEY_LOCAL_MACHINE isn't being accessed. Set the SubKey to the name of the key to open or create (eg. "\Software\MyCompany\MyProduct"). Set the ValueName to the name of the value to set or query (eg. "Colors"). Use the Value property to set or query the value.
The sample code illustrates the use of this class. For a more detailed example refer to the example code in RegEx4.
Property ComputerName As String |
Property HKEY As Long |
Property SubKey As String |
Property ValueName As String |
Property Value As Variant |
The ComputerName identifies the computer whose registry database is to be accessed. If no name is specified then the local database is assumed.
Dim Reg As New RegistryAccessor
Reg.ComputerName = "WebServer"
The HKEY is the handle of the key whose subkey is to be created or opened. If no handle is specified then REG_HKEY_LOCAL_MACHINE is assumed.
Dim Reg As New RegistryAccessor
Reg.HKEY = REG_HKEY_CURRENT_USER
The SubKey is the name of the subkey to create or open.
Dim Reg As New RegistryAccessor
Reg.SubKey = "\Software\MyCompany\MyProduct"
The ValueName is the name of the value to set or query.
Dim Reg As New RegistryAccessor
Reg.ValueName = "Colors"
The Value contains the data to set or the results of the query. The data type determines the registry value type when setting the value. The registry value type determines the data type when querying the value.
Dim Reg As New RegistryAccessor
Reg.Value = 123
The RegistryDatabase class is used to access registry keys. For simple access use the RegistryAccessor class.
To use the RegistryDatabase create an object using the new operator or the CreateObject function. Set the ComputerName if a remote computer is being accessed. Use one of the methods to create, open , load, unload or delete a registry key.
The sample code illustrates the use of this class. For a more detailed example of creating, opening, deleting keys, as well as setting and querying values refer to the example code in RegEx1. For an example of enumerating registry keys and values refer to the example code in RegEx2.
Property ComputerName As String |
Function CreateKey(hKeyBase As Long, SubKey As String) As RegistryKey |
Sub DeleteKey(hKeyBase As Long, SubKey As String) |
Sub DeleteKeyTree(hKeyBase As Long, SubKey As String) |
Function LoadKey(hKeyBase As Long, SubKey As String, FileName As String) As RegistryKey |
Function OpenKey(hKeyBase As Long, SubKey As String, Optional AccessMode as Long = 0) As RegistryKey |
Sub UnloadKey(hKeyBase As Long, SubKey As String) |
The ComputerName identifies the computer whose registry database is to be accessed. If no name is specified then the local database is assumed. If a remote computer is to be accessed then this property should be set prior to invoking any of the methods.
Dim RemoteRegDB As New RegistryDatabase
RemoteRegDB.ComputerName = "WebServer"
Create a new or open an existing key. The hKeyBase is any one of the predefined HKEYs. The SubKey is the name of the key to create. If the SubKey string is empty then the hKeyBase is opened.
Dim RegKey As RegistryKey
' Open HKEY_LOCAL_MACHINE
Set RegKey = RegDB.CreateKey(REG_HKEY_LOCAL_MACHINE, "")
' Create or open a key under HKEY_LOCAL_MACHINE
Set RegKey = RegDB.CreateKey(REG_HKEY_LOCAL_MACHINE, "\Software\MyCompany\MyProduct")
Delete a key. The hKeyBase is any one of the predefined HKEYs. The SubKey is the name of the key to delete.
Under Windows NT if the key to be deleted has subkeys then the operation fails. To delete a key and all its subkeys use the DeleteKeyTree method.
' Delete a key under HKEY_LOCAL_MACHINE
RegDB.DeleteKey REG_HKEY_LOCAL_MACHINE, "\Software\MyCompany\MyProduct"
Recursively delete a key and any child subkeys. The hKeyBase is any one of the predefined HKEYs. The SubKey is the name of the key to delete.
' Delete a key and all subkeys under HKEY_LOCAL_MACHINE
RegDB.DeleteKeyTree REG_HKEY_LOCAL_MACHINE, "\Software\MyCompany\MyProduct"
Load a key, its subkeys and values from a file. The hKeyBase is either REG_HKEY_LOCAL_MACHINE or REG_HKEY_USERS. The SubKey is the name of the key to create using the information stored in the file previously created using Save. To support all file systems, the file name should not include an extension.
Dim RegKey As RegistryKey
' Load a previously saved key
Set RegKey = RegDB.LoadKey(REG_HKEY_LOCAL_MACHINE, "\Software\MyCompany\MyProduct", "KeyFile")
Open an existing key. The hKeyBase is any one of the predefined HKEYs. The SubKey is the name of the key to open. If the SubKey string is empty then the hKeyBase is opened. The AccessMode is any one of the predefined modes. The default AccessMode is REG_ACCESS_BEST_AVAILABLE.
Dim RegKey As RegistryKey
' Open HKEY_LOCAL_MACHINE
Set RegKey = RegDB.OpenKey(REG_HKEY_LOCAL_MACHINE, "")
' Open a key under HKEY_LOCAL_MACHINE with default access
Set RegKey = RegDB.OpenKey(REG_HKEY_LOCAL_MACHINE, "\Software\MyCompany\MyProduct")
' Open a key under HKEY_LOCAL_MACHINE with read only access
Set RegKey = RegDB.OpenKey(REG_HKEY_LOCAL_MACHINE, "\Software\MyCompany\MyProduct", REG_ACCESS_READ)
' Open a key under HKEY_LOCAL_MACHINE with full access
Set RegKey = RegDB.OpenKey(REG_HKEY_LOCAL_MACHINE, "\Software\MyCompany\MyProduct", REG_ACCESS_ALL)
Unload from the registry a key, its subkeys and values previously loaded using LoadKey. The hKeyBase is either REG_HKEY_LOCAL_MACHINE or REG_HKEY_USERS. The SubKey is the name of the key to unload.
Dim RegKey As RegistryKey
' Unload a previously loaded key
RegDB.UnloadKey REG_HKEY_LOCAL_MACHINE, "\Software\MyCompany\MyProduct"
The following HKEYs are defined:
REG_HKEY_CLASSES_ROOT
REG_HKEY_CURRENT_CONFIG
REG_HKEY_CURRENT_USER
REG_HKEY_DYN_DATA
REG_HKEY_LOCAL_MACHINE
REG_HKEY_PERFORMANCE_DATA
REG_HKEY_USERS
Note when accessing remote computers only REG_HKEY_LOCAL_MACHINE and REG_HKEY_USERS are available.
The following access modes are defined:
REG_ACCESS_BEST_AVAILABLE
REG_ACCESS_ALL
REG_ACCESS_CREATE_LINK
REG_ACCESS_CREATE_SUB_KEYS
REG_ACCESS_ENUM_SUB_KEYS
REG_ACCESS_EXECUTE
REG_ACCESS_NOTIFY
REG_ACCESS_QUERY_VALUE
REG_ACCESS_READ
REG_ACCESS_SET_VALUE
REG_ACCESS_WRITE
When opening a key with REG_ACCESS_BEST_AVAILABLE first REG_ACCESS_ALL is tried. If this fails REG_ACCESS_WRITE and then REG_ACCESS_READ are tried before the attempt to open the key is failed.
Access modes are only applicable to Windows NT and can be ignored for Windows 95/98.
The RegistryKey class is used to manipulate registry keys and access registry values.
The RegistryKey cannot be directly created. Instead RegistryKey references are returned by the RegistryDatabase class's CreateKey, LoadKey and OpenKey.
The RegistryKey class allows access to registry values via the OpenValue method. Registry keys and key names may be enumerated using SubKeys and SubKeyNames respectively. Registry values and value names may be enumerated using Values and ValueNames respectively. Notification of registry changes may be received using the OnRegistryChange event.
The sample code illustrates the use of this class. For a more detailed example of creating, opening, deleting keys, as well as setting and querying values refer to the example code in RegEx1. For an example of enumerating registry keys and values refer to the example code in RegEx2.
Sub DeleteValue(ValueName As String) |
Sub Flush() |
Property HKEY As Long ' Read only |
Property Name As String ' Read only |
Event OnRegistryChange() |
Function OpenValue(ValueName As String) As RegistryValue |
Property RegistryDatabase As RegistryDatabase ' Read only |
Sub Replace(NewFileName As String, BackupFileName As String) |
Sub Restore(FileName As String) |
Sub Save(FileName As String) |
Property SubKeyCount As Long ' Read only |
Property SubKeyNames As Variant ' Read only |
Function SubKeys() |
Property ValueCount As Long ' Read only |
Property ValueNames As Variant ' Read only |
Function Values() |
Delete a value. The ValueName is the name of the value to delete.
' Delete a value
RegKey.DeleteKey "Colors"
Flush a key and all its subkeys and values to the registry database on disk. Normally this isn't required as it occurs automatically.
' Flush a key
RegKey.Flush
Return the key handle associated with this open key.
' Get the key handle
Dim hKey As Long
hKey = RegKey.HKEY
Return the key name. The name is relative to the HKEY used when it was opened.
' Get the key name
Dim KeyName As String
KeyName = RegKey.Name
The OnRegistryChange event is fired when a key or one of its subkeys or values changes, is added or deleted. Registry change notification is only applicable to the local registry database.
' Open a value
Private WithEvents RegKey As RegistryKey
Set RegKey = RegDB.OpenKey(REG_HKEY_LOCAL_MACHINE, "\Software\MyCompany\MyProduct")
' Receive registry change event
Private Sub RegKey_OnRegistryChange()
' Do something with RegKey
End Sub
Open a value. The ValueName is the name of the value to open.
' Open a value
Dim RegValue As RegistryValue
Set RegValue = RegKey.OpenValue("Colors")
Return the parent RegistryDatabase object.
' Get the parent registry database object
Dim RegDB As RegistryDatabase
Set RegDB = RegKey.RegistryDatabase
Save the key and its subkeys and values to a file.
Load a key, its subkeys and values from a file when the system is next started. The replaced key information is saved in the backup file. To support all file systems, the file names should not include an extension.
' Replace the key
RegKey.Replace "KeyFile" "OldKeyFile"
Restore a key, its subkeys and values from a file previously created using Save. To support all file systems, the file name should not include an extension.
' Restore a previously saved key
RegKey.Restore "KeyFile"
Save the key and its subkeys and values to a file.
' Save the key to file
RegKey.Save "KeyFile"
Return the number of subkeys.
' Get the subkey count
Dim Count As Long
Count = RegKey.SubKeyCount
Return an array of subkey names.
' Get the subkey names
Dim SubKeyNames() As String
SubKeyNames = RegKey.SubKeyNames
Return an enumeration of subkeys.
' Get the subkeys
Dim SubKey As RegistryKey
For Each SubKey In RegKey.SubKeys
Next
' Or alternatively, get the subkeys
For i = 1 To RegKey.SubKeys.Count
Set SubKey = RegKey.SubKeys(i)
Next
Return the number of values.
' Get the value count
Dim Count As Long
Count = RegKey.ValueCount
Return an array of value names.
' Get the value names
Dim ValueNames() As String
ValueNames = RegKey.ValueNames
Return an enumeration of values.
' Get the values
Dim RegValue As RegistryValue
For Each RegValue In RegKey.Values
Next
' Or alternatively, get the values
For i = 1 To RegKey.Values.Count
Set RegValue = RegKey.Values(i)
Next
The RegistryValue class is used to manipulate registry values.
The RegistryValue cannot be directly created. Instead RegistryValue references are returned by the RegistryKey class's OpenValue. The Value property is used to set and get registry values.
On a set, the variant type of the variable being assigned determines the registry value type. Only long integers, strings, string arrays and byte arrays are supported by the Windows registry. To store other types of data first requires it to be converted to an appropriate type prior to setting the registry value.
On a get, the type of the returned variant is determined by the registry value type. The ValueType property may be used to determine the type of value prior to retrieving it.
The sample code illustrates the use of this class. For a more detailed example of creating, opening, deleting keys, as well as setting and querying values refer to the example code in RegEx1.
Property ExpandString As Boolean |
Property Name As String ' Read only |
Property RegistryKey As RegistryKey ' Read only |
Property Value As Variant |
Property ValueType As Long ' Read only |
Get or set the flag indicating an expanded string. This is used to distinguish between string and expanded string value types. The default setting is false.
' Get and set the expanded string flag
Dim Expanded As Boolean
Expanded = RegValue.ExpandedString
RegValue.ExpandedString = True
Return the value name.
' Get the value name
Dim ValueName As String
ValueName = RegValue.Name
Return the parent RegistryKey object.
' Get the parent registry key object
Dim RegKey As RegistryKey
Set RegKey = RegValue.RegistryKey
Get or set the value. Value is the default property. The ValueType property may be used to determine the type of value prior to retrieving it.
' Set a binary value
Dim ByteArray(1 To 128) As Byte
RegValue = ByteArray
' Set an integer value
RegValue = 123
' Set an expanded string value
RegValue.ExpandedString = True
RegValue = "123"
' Set a multi-string value
RegValue = Array("123", "456", "789")
' Set a string value
RegValue.ExpandedString = False
RegValue = "123"
' Get a binary value
Dim v As Variant
v = RegValue
' Get an integer value
Dim l As Long
l = RegValue
' Get a multi-string value
Dim v As Variant
v = RegValue
' Get a string or expanded string value
Dim StringValue As String
Dim Expanded As Boolean
StringValue = RegValue
Expanded = RegValue.ExpandedString
Return the value type.
' Get the value type
Dim ValueType As Long
ValueType = RegValue.ValueType
The following value types are defined:
REG_VALUE_BINARY | Array of bytes |
REG_VALUE_DWORD | Long integer |
REG_VALUE_EXPAND_SZ | String containing unexpanded references to environment variables (eg. %PATH%) |
REG_VALUE_MULTI_SZ | Array of strings |
REG_VALUE_SZ | String |
Sample code, developed under Visual Basic 6.0, is included with the control. If you wish to build this code, don't forget to include the control using the Project | References dialog.
RegEx1 demonstrates creating, opening, deleting keys, as well as setting and querying values.
RegEx2 demonstrates enumeration of registry keys and values.
RegEx3 demonstrates registry events.
RegEx4 demonstrates using the registry accessor class.
RegEx5 demonstrates using the registry control on a remote server using CreateObject. This requires updating the DCOM configuration.
RegEx6 demonstrates using the registry control from within an HTML page. These examples require Internet Explorer 4.0 or a later release.
Under most circumstances the registry control will be used as an inproc server as access to remote registries may be achieved using the ComputerName property. However the control can be accessed as a remote out-of-proc server using a DLL surrogate (eg. DllHost.exe).
The registry on the remote server must be updated to include an AppID for each registry object class to be accessed as well as an AppID under the corresponding CLSID entry.
For example, to access the RegistryAccessor object class remotely update the server's registry by:
Run DCOMCNFG on the remote server to set the access, launch, and configuration permissions as well as the user account.